package org.tib.concurrent;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Imran
 * Date: 19-Aug-2010
 * Time: 22:26:38
 * To change this template use File | Settings | File Templates.
 */
public class FibonaciProcessor extends Processor<Map<String,Integer>,MathComputeResult> {
    public final static String METHOD = "method";
    public final static String SEQUENCE = "sequence";
    Map<String,Integer> parameters;

    @Override
    public MathComputeResult executeTask() throws ConcurrentException {
        MathComputeResult result = new MathComputeResult();

        if(this.parameters != null && !this.parameters.isEmpty()){
            Fibonaci fib = new Fibonaci();
            if(Fibonaci.LOOP_FIBONACI.equals(parameters.get(METHOD))) {
                Map<String,Number> resultMap = new HashMap<String,Number>();
                resultMap.put(MathComputeResult.COMPUTATION, Fibonaci.LOOP_FIBONACI);
                resultMap.put(MathComputeResult.RESULT, fib.computeFibonaci(parameters.get(SEQUENCE)));
                result.setResult(resultMap);
            }
            else if(Fibonaci.RECURSION_FIBONACI.equals(parameters.get(METHOD))) {
                Map<String,Number> resultMap = new HashMap<String,Number>();
                resultMap.put(MathComputeResult.COMPUTATION, Fibonaci.RECURSION_FIBONACI);
                resultMap.put(MathComputeResult.RESULT, fib.computeFibonaciRecursion(parameters.get(SEQUENCE)));
                result.setResult(resultMap);
            }
            else {
                throw new ConcurrentException("Unknown computation type. Consult the Fibonaci implementation for allowed computations!!");
            }
        }
        else {
            throw new ConcurrentException("Cannot have null values for computations!!");
        }
        return result;
    }

    @Override
    public void setParam(Map<String,Integer> param) {
        this.parameters = param;
    }
}
